import 'package:flutter/material.dart';

class CustomAppbar extends StatefulWidget implements PreferredSizeWidget {
  final double contentHeight; //从外部指定高度
  Color navigationBarBackgroundColor; //设置导航栏背景的颜色
  Widget leadingWidget;
  String title;
  List<Widget>? rightWidgets;

  bool iscustom ;//是否是自定义

  CustomAppbar({Key? key, required this.leadingWidget, required this.title, this.contentHeight = 56, this.navigationBarBackgroundColor = Colors.white,
    this.rightWidgets,this.iscustom=false})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CustomAppbarState();
  }

  @override
  Size get preferredSize => Size.fromHeight(contentHeight);
}

/// 这里没有直接用SafeArea，而是用Container包装了一层
/// 因为直接用SafeArea，会把顶部的statusBar区域留出空白
/// 外层Container会填充SafeArea，指定外层Container背景色也会覆盖原来SafeArea的颜色
///     var statusheight = MediaQuery.of(context).padding.top;  获取状态栏高度

class _CustomAppbarState extends State<CustomAppbar> {
  @override
  void initState() {
    super.initState();
    widget.rightWidgets ??= [];
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.navigationBarBackgroundColor,
      child: SafeArea(
        top: true,
        child: Container(
            decoration: const UnderlineTabIndicator(
              borderSide: BorderSide(width: 1.0, color: Color(0xFFeeeeee)),
            ),
            height: widget.contentHeight,
            child: Stack(
              alignment: Alignment.center,
              children: <Widget>[
                Positioned(
                  left: 10,
                  child: Container(
                    padding: const EdgeInsets.only(left: 5),
                    child: widget.leadingWidget,
                  ),
                ),

                Visibility(
                  visible: !widget.iscustom,
                  child: Container(
                    child: Text(widget.title, style: const TextStyle(fontSize: 17, color: Color(0xFF333333))),
                  ),
                ),
                Positioned(
                  right: 0,
                  child: Container(
                    margin: const EdgeInsets.only(right: 14),
                    child: Row(
                      children: widget.rightWidgets!,
                    ),
                  ),
                ),
              ],
            )),
      ),
    );
  }
}
